package com.jiang.daily.Q2502;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/25 17:48
 */
class Allocator {
    int[] arr;

    public Allocator(int n) {
        arr = new int[n];
    }

    public int allocate(int size, int mID) {
        int index = 0;
        int cnt = size;
        while (index < arr.length) {
            if (arr[index] == 0) {
                cnt--;
                if (cnt == 0) {
                    for (int i = index - size + 1; i <= index; i++) {
                        arr[i] = mID;
                    }
                    return index - size + 1;
                }
            } else {
                cnt = size;
            }

            index++;
        }
        return -1;
    }

    public int freeMemory(int mID) {
        int cnt = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == mID) {
                arr[i] = 0;
                cnt++;
            }
        }
        return cnt;
    }
}


